<!DOCTYPE html>

<html lang="en">
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>教程 7：创建简单的 REST API（Tutorial 7: Creating a Simple REST API） &mdash; Phalcon 3.0.1 文档</title>
    <meta name="keywords" content="php, phalcon, phalcon php, php framework, faster php framework">
    <link rel="stylesheet" type="text/css" href="http://cdn.staticfile.org/twitter-bootstrap/3.3.6/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="/www/css/phalcon.min.css" />
    <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400' rel='stylesheet' type='text/css'>
    <link href="//fonts.googleapis.com/css?family=Merriweather:400,700" rel="stylesheet" type="text/css" />
    <!--
    EUROPE <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400&subset=latin-ext' rel='stylesheet' type='text/css'>
    GREEK <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400&subset=greek-ext' rel='stylesheet' type='text/css'>
    RUSSIA <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400&subset=cyrillic-ext,latin' rel='stylesheet' type='text/css'>
    -->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <link rel="stylesheet" href="../static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../static/docs.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>

    <script src="http://cdn.staticfile.org/jquery/1.8.1/jquery.min.js"></script>
    <script type="text/javascript" src="../static/docs.js"></script>
    <link rel="shortcut icon" href="../static/favicon.ico"/>
    <link rel="top" title="Phalcon 3.0.1 文档" href="../index.html" />
    <link rel="next" title="示例列表（List of examples）" href="samples.html" />
    <link rel="prev" title="Tutorial 6: Vökuró" href="tutorial-vokuro.html" /> 
  </head>
  <body>

<header class="page-header">
    <nav class="navbar" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-menu-container">
                    <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
                </button>
                <a class="navbar-brand phalcon-logo" href="/"><span itemprop="name" class="sr-only">Phalcon PHP</span></a>
            </div>

            <div class="collapse navbar-collapse navbar-right" id="main-menu-container">
                <ul class="nav navbar-nav main-menu">
                  <li class="first"><a href="//phalconphp.com/en/download" class="header-nav-link">Download</a></li>
                  <li><a href="//docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                  <li><a href="//forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                  <li><a href="//blog.phalconphp.com/" class="header-nav-link" target="_blank">Blog</a></li>
                  <li><a href="//phalconist.com/" class="header-nav-link" target="_blank">Resources</a></li>
                  <li><a href="//phalconphp.com/en/about">About</a></li>
                  <li><div align="right">
                      <iframe src="https://ghbtns.com/github-btn.html?user=phalcon&amp;repo=cphalcon&amp;type=watch&amp;count=true&amp;size=large"
      allowtransparency="true" frameborder="0" scrolling="0" width="152px" height="30px"></iframe>
                    </div></li>
                </ul>
            </div>
        </div>
    </nav>
  </header>

<div class="heading">
    <div class="container">
        <div class="row">
            <h2>Documentation</h2>
        </div>
    </div>
</div>
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="samples.html" title="示例列表（List of examples）"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="tutorial-vokuro.html" title="Tutorial 6: Vökuró"
             accesskey="P">上一页</a> |</li>
        <li><a href="//phalconphp.com">Home</a> &raquo;</li>
        <li><a href="../index.html">Phalcon 3.0.1 文档</a> &raquo;</li> 
      </ul>
    </div>  

      <table width="100%" align="center" cellpadding="0" cellspacing="0">
        <tr>
      <td class="primary-box" width="25%" valign="top">
            <div>
            <div id="searchbox" style="">
                <!--<form class="search" action="http://readthedocs.org/search/project/" method="get">
                  <input type="search" name="q" size="25" placeholder="Search">
                  <input type="submit" value="Go">
                  <input type="hidden" name="selected_facets" value="project:">
                </form>-->
                <div style="width:200px;padding:10px">
                  <gcse:searchbox-only></gcse:searchbox-only>
                </div>
            </div>
            </div>
            <div style="padding:5px;padding-left:10px">
              <div id="carbonads-container">
                <div class="carbonad"><div id="azcarbon"></div>
                <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?zoneid=1673&serve=C6AILKT&placement=phalconphpcom" id="_carbonads_js"></script>
                </div></div>
            </div>
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference internal" href="#">教程 7：创建简单的 REST API（Tutorial 7: Creating a Simple REST API）</a><ul>
<li><a class="reference internal" href="#api-defining-the-api">定义 API（Defining the API）</a></li>
<li><a class="reference internal" href="#creating-the-application">创建应用（Creating the Application）</a></li>
<li><a class="reference internal" href="#creating-a-model">创建模型（Creating a Model）</a></li>
<li><a class="reference internal" href="#retrieving-data">检索数据（Retrieving Data）</a></li>
<li><a class="reference internal" href="#inserting-data">插入数据（Inserting Data）</a></li>
<li><a class="reference internal" href="#updating-data">更新数据（Updating Data）</a></li>
<li><a class="reference internal" href="#deleting-data">删除数据（Deleting Data）</a></li>
<li><a class="reference internal" href="#testing-our-application">测试应用（Testing our Application）</a></li>
<li><a class="reference internal" href="#conclusion">结束语（Conclusion）</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="tutorial-vokuro.html" title="上一章">&lt; Tutorial 6: Vökuró</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="samples.html" title="下一章">示例列表（List of examples） &gt;</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../sources/reference/tutorial-rest.md" rel="nofollow">显示源代码</a></li>
            </ul>
        </td>
          <td class="second-box" valign="top">
            <div class="document">
                <div class="documentwrapper">
                  <div class="bodywrapper">
                    <div class="body" >
                      
  <div class="section" id="rest-api-tutorial-7-creating-a-simple-rest-api">
<h1>教程 7：创建简单的 REST API（Tutorial 7: Creating a Simple REST API）<a class="headerlink" href="#rest-api-tutorial-7-creating-a-simple-rest-api" title="永久链接至标题">¶</a></h1>
<p>在这个教程中，我们会学习如何创建一个拥有 <a class="reference external" href="http://zh.wikipedia.org/wiki/REST">RESTful</a> API 的应用程序，它将会使用如下的几个 HTTP 方法：</p>
<ul class="simple">
<li>GET - 接受、查找数据</li>
<li>POST - 添加数据</li>
<li>PUT - 更新数据</li>
<li>DELETE - 删除数据</li>
</ul>
<div class="section" id="api-defining-the-api">
<h2>定义 API（Defining the API）<a class="headerlink" href="#api-defining-the-api" title="永久链接至标题">¶</a></h2>
<p>这个 API 包含如下方法(Methods)</p>
<table border="1" class="docutils">
<colgroup>
<col width="9%" />
<col width="30%" />
<col width="62%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Method</th>
<th class="head">URL</th>
<th class="head">Action</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>GET</td>
<td>/api/robots</td>
<td>Retrieves all robots</td>
</tr>
<tr class="row-odd"><td>GET</td>
<td>/api/robots/search/Astro</td>
<td>Searches for robots with ‘Astro’ in their name</td>
</tr>
<tr class="row-even"><td>GET</td>
<td>/api/robots/2</td>
<td>Retrieves robots based on primary key</td>
</tr>
<tr class="row-odd"><td>POST</td>
<td>/api/robots</td>
<td>Adds a new robot</td>
</tr>
<tr class="row-even"><td>PUT</td>
<td>/api/robots/2</td>
<td>Updates robots based on primary key</td>
</tr>
<tr class="row-odd"><td>DELETE</td>
<td>/api/robots/2</td>
<td>Deletes robots based on primary key</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="creating-the-application">
<h2>创建应用（Creating the Application）<a class="headerlink" href="#creating-the-application" title="永久链接至标题">¶</a></h2>
<p>As the application is so simple, we will not implement any full MVC environment to develop it. In this case,
we will use a <a class="reference internal" href="micro.html"><em>micro application</em></a> to meet our goal.</p>
<p>The following file structure is more than enough:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="x">my-rest-api/</span>
<span class="x">    models/</span>
<span class="x">        Robots.php</span>
<span class="x">    index.php</span>
<span class="x">    .htaccess</span>
</pre></div>
</div>
<p>First, we need an .htaccess file that contains all the rules to rewrite the URIs to the index.php file,
that is our application:</p>
<div class="highlight-apacheconf"><div class="highlight"><pre><span class="nt">&lt;IfModule</span> <span class="s">mod_rewrite.c</span><span class="nt">&gt;</span>
    <span class="nb">RewriteEngine</span> <span class="k">On</span>
    <span class="nb">RewriteCond</span> %{REQUEST_FILENAME} !-f
    <span class="nb">RewriteRule</span> ^((?s).*)$ index.php?_url=/$1 [QSA,L]
<span class="nt">&lt;/IfModule&gt;</span>
</pre></div>
</div>
<p>Then, in the index.php file we create the following:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Micro</span><span class="p">;</span>

<span class="nv">$app</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Micro</span><span class="p">();</span>

<span class="c1">// Define the routes here</span>

<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">();</span>
</pre></div>
</div>
<p>Now we will create the routes as we defined above:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Micro</span><span class="p">;</span>

<span class="nv">$app</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Micro</span><span class="p">();</span>

<span class="c1">// Retrieves all robots</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">()</span> <span class="p">{</span>

    <span class="p">}</span>
<span class="p">);</span>

<span class="c1">// Searches for robots with $name in their name</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots/search/{name}&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">(</span><span class="nv">$name</span><span class="p">)</span> <span class="p">{</span>

    <span class="p">}</span>
<span class="p">);</span>

<span class="c1">// Retrieves robots based on primary key</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots/{id:[0-9]+}&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">(</span><span class="nv">$id</span><span class="p">)</span> <span class="p">{</span>

    <span class="p">}</span>
<span class="p">);</span>

<span class="c1">// Adds a new robot</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">post</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">()</span> <span class="p">{</span>

    <span class="p">}</span>
<span class="p">);</span>

<span class="c1">// Updates robots based on primary key</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">put</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots/{id:[0-9]+}&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">()</span> <span class="p">{</span>

    <span class="p">}</span>
<span class="p">);</span>

<span class="c1">// Deletes robots based on primary key</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots/{id:[0-9]+}&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">()</span> <span class="p">{</span>

    <span class="p">}</span>
<span class="p">);</span>

<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">();</span>
</pre></div>
</div>
<p>Each route is defined with a method with the same name as the HTTP method, as first parameter we pass a route pattern,
followed by a handler. In this case, the handler is an anonymous function. The following route: <code class="code docutils literal"><span class="pre">'/api/robots/{id:[0-9]+}'</span></code>,
by example, explicitly sets that the &#8220;id&#8221; parameter must have a numeric format.</p>
<p>When a defined route matches the requested URI then the application executes the corresponding handler.</p>
</div>
<div class="section" id="creating-a-model">
<h2>创建模型（Creating a Model）<a class="headerlink" href="#creating-a-model" title="永久链接至标题">¶</a></h2>
<p>Our API provides information about &#8216;robots&#8217;, these data are stored in a database. The following model allows us to
access that table in an object-oriented way. We have implemented some business rules using built-in validators
and simple validations. Doing this will give us the peace of mind that saved data meet the requirements of our
application:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model\Message</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model\Validator\Uniqueness</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model\Validator\InclusionIn</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Robots</span> <span class="k">extends</span> <span class="nx">Model</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">validation</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// Type must be: droid, mechanical or virtual</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">validate</span><span class="p">(</span>
            <span class="k">new</span> <span class="nx">InclusionIn</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;field&quot;</span>  <span class="o">=&gt;</span> <span class="s2">&quot;type&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;domain&quot;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                        <span class="s2">&quot;droid&quot;</span><span class="p">,</span>
                        <span class="s2">&quot;mechanical&quot;</span><span class="p">,</span>
                        <span class="s2">&quot;virtual&quot;</span><span class="p">,</span>
                    <span class="p">]</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="p">);</span>

        <span class="c1">// Robot name must be unique</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">validate</span><span class="p">(</span>
            <span class="k">new</span> <span class="nx">Uniqueness</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;field&quot;</span>   <span class="o">=&gt;</span> <span class="s2">&quot;name&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;message&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;The robot name must be unique&quot;</span><span class="p">,</span>
                <span class="p">]</span>
            <span class="p">)</span>
        <span class="p">);</span>

        <span class="c1">// Year cannot be less than zero</span>
        <span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">year</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
            <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">appendMessage</span><span class="p">(</span>
                <span class="k">new</span> <span class="nx">Message</span><span class="p">(</span><span class="s2">&quot;The year cannot be less than zero&quot;</span><span class="p">)</span>
            <span class="p">);</span>
        <span class="p">}</span>

        <span class="c1">// Check if any messages have been produced</span>
        <span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">validationHasFailed</span><span class="p">()</span> <span class="o">==</span> <span class="k">true</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span> <span class="k">false</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Now, we must set up a connection to be used by this model and load it within our app:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Loader</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Micro</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Di\FactoryDefault</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Db\Adapter\Pdo\Mysql</span> <span class="k">as</span> <span class="nx">PdoMysql</span><span class="p">;</span>

<span class="c1">// Use Loader() to autoload our model</span>
<span class="nv">$loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Loader</span><span class="p">();</span>

<span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">registerDirs</span><span class="p">(</span>
    <span class="p">[</span>
        <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s2">&quot;/models/&quot;</span>
    <span class="p">]</span>
<span class="p">)</span><span class="o">-&gt;</span><span class="na">register</span><span class="p">();</span>

<span class="nv">$di</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">FactoryDefault</span><span class="p">();</span>

<span class="c1">// Set up the database service</span>
<span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span>
    <span class="s2">&quot;db&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">()</span> <span class="p">{</span>
        <span class="k">return</span> <span class="k">new</span> <span class="nx">PdoMysql</span><span class="p">(</span>
            <span class="p">[</span>
                <span class="s2">&quot;host&quot;</span>     <span class="o">=&gt;</span> <span class="s2">&quot;localhost&quot;</span><span class="p">,</span>
                <span class="s2">&quot;username&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;asimov&quot;</span><span class="p">,</span>
                <span class="s2">&quot;password&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;zeroth&quot;</span><span class="p">,</span>
                <span class="s2">&quot;dbname&quot;</span>   <span class="o">=&gt;</span> <span class="s2">&quot;robotics&quot;</span><span class="p">,</span>
            <span class="p">]</span>
        <span class="p">);</span>
    <span class="p">}</span>
<span class="p">);</span>

<span class="c1">// Create and bind the DI to the application</span>
<span class="nv">$app</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Micro</span><span class="p">(</span><span class="nv">$di</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="retrieving-data">
<h2>检索数据（Retrieving Data）<a class="headerlink" href="#retrieving-data" title="永久链接至标题">¶</a></h2>
<p>The first &#8220;handler&#8221; that we will implement is which by method GET returns all available robots. Let&#8217;s use PHQL to
perform this simple query returning the results as JSON:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="c1">// Retrieves all robots</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">()</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$app</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$phql</span> <span class="o">=</span> <span class="s2">&quot;SELECT * FROM Robots ORDER BY name&quot;</span><span class="p">;</span>

        <span class="nv">$robots</span> <span class="o">=</span> <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">modelsManager</span><span class="o">-&gt;</span><span class="na">executeQuery</span><span class="p">(</span><span class="nv">$phql</span><span class="p">);</span>

        <span class="nv">$data</span> <span class="o">=</span> <span class="p">[];</span>

        <span class="k">foreach</span> <span class="p">(</span><span class="nv">$robots</span> <span class="k">as</span> <span class="nv">$robot</span><span class="p">)</span> <span class="p">{</span>
            <span class="nv">$data</span><span class="p">[]</span> <span class="o">=</span> <span class="p">[</span>
                <span class="s2">&quot;id&quot;</span>   <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">id</span><span class="p">,</span>
                <span class="s2">&quot;name&quot;</span> <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">name</span><span class="p">,</span>
            <span class="p">];</span>
        <span class="p">}</span>

        <span class="k">echo</span> <span class="nb">json_encode</span><span class="p">(</span><span class="nv">$data</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">);</span>
</pre></div>
</div>
<p><a class="reference internal" href="phql.html"><em>PHQL</em></a>, allow us to write queries using a high-level, object-oriented SQL dialect that internally
translates to the right SQL statements depending on the database system we are using. The clause &#8220;use&#8221; in the
anonymous function allows us to pass some variables from the global to local scope easily.</p>
<p>The searching by name handler would look like:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="c1">// Searches for robots with $name in their name</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots/search/{name}&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">(</span><span class="nv">$name</span><span class="p">)</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$app</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$phql</span> <span class="o">=</span> <span class="s2">&quot;SELECT * FROM Robots WHERE name LIKE :name: ORDER BY name&quot;</span><span class="p">;</span>

        <span class="nv">$robots</span> <span class="o">=</span> <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">modelsManager</span><span class="o">-&gt;</span><span class="na">executeQuery</span><span class="p">(</span>
            <span class="nv">$phql</span><span class="p">,</span>
            <span class="p">[</span>
                <span class="s2">&quot;name&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;%&quot;</span> <span class="o">.</span> <span class="nv">$name</span> <span class="o">.</span> <span class="s2">&quot;%&quot;</span>
            <span class="p">]</span>
        <span class="p">);</span>

        <span class="nv">$data</span> <span class="o">=</span> <span class="p">[];</span>

        <span class="k">foreach</span> <span class="p">(</span><span class="nv">$robots</span> <span class="k">as</span> <span class="nv">$robot</span><span class="p">)</span> <span class="p">{</span>
            <span class="nv">$data</span><span class="p">[]</span> <span class="o">=</span> <span class="p">[</span>
                <span class="s2">&quot;id&quot;</span>   <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">id</span><span class="p">,</span>
                <span class="s2">&quot;name&quot;</span> <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">name</span><span class="p">,</span>
            <span class="p">];</span>
        <span class="p">}</span>

        <span class="k">echo</span> <span class="nb">json_encode</span><span class="p">(</span><span class="nv">$data</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">);</span>
</pre></div>
</div>
<p>Searching by the field &#8220;id&#8221; it&#8217;s quite similar, in this case, we&#8217;re also notifying if the robot was found or not:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Http\Response</span><span class="p">;</span>

<span class="c1">// Retrieves robots based on primary key</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots/{id:[0-9]+}&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">(</span><span class="nv">$id</span><span class="p">)</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$app</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$phql</span> <span class="o">=</span> <span class="s2">&quot;SELECT * FROM Robots WHERE id = :id:&quot;</span><span class="p">;</span>

        <span class="nv">$robot</span> <span class="o">=</span> <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">modelsManager</span><span class="o">-&gt;</span><span class="na">executeQuery</span><span class="p">(</span>
            <span class="nv">$phql</span><span class="p">,</span>
            <span class="p">[</span>
                <span class="s2">&quot;id&quot;</span> <span class="o">=&gt;</span> <span class="nv">$id</span><span class="p">,</span>
            <span class="p">]</span>
        <span class="p">)</span><span class="o">-&gt;</span><span class="na">getFirst</span><span class="p">();</span>



        <span class="c1">// Create a response</span>
        <span class="nv">$response</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Response</span><span class="p">();</span>

        <span class="k">if</span> <span class="p">(</span><span class="nv">$robot</span> <span class="o">===</span> <span class="k">false</span><span class="p">)</span> <span class="p">{</span>
            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setJsonContent</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;status&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;NOT-FOUND&quot;</span>
                <span class="p">]</span>
            <span class="p">);</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setJsonContent</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;status&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;FOUND&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;data&quot;</span>   <span class="o">=&gt;</span> <span class="p">[</span>
                        <span class="s2">&quot;id&quot;</span>   <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">id</span><span class="p">,</span>
                        <span class="s2">&quot;name&quot;</span> <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">name</span>
                    <span class="p">]</span>
                <span class="p">]</span>
            <span class="p">);</span>
        <span class="p">}</span>

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="inserting-data">
<h2>插入数据（Inserting Data）<a class="headerlink" href="#inserting-data" title="永久链接至标题">¶</a></h2>
<p>Taking the data as a JSON string inserted in the body of the request, we also use PHQL for insertion:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Http\Response</span><span class="p">;</span>

<span class="c1">// Adds a new robot</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">post</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">()</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$app</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$robot</span> <span class="o">=</span> <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">request</span><span class="o">-&gt;</span><span class="na">getJsonRawBody</span><span class="p">();</span>

        <span class="nv">$phql</span> <span class="o">=</span> <span class="s2">&quot;INSERT INTO Robots (name, type, year) VALUES (:name:, :type:, :year:)&quot;</span><span class="p">;</span>

        <span class="nv">$status</span> <span class="o">=</span> <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">modelsManager</span><span class="o">-&gt;</span><span class="na">executeQuery</span><span class="p">(</span>
            <span class="nv">$phql</span><span class="p">,</span>
            <span class="p">[</span>
                <span class="s2">&quot;name&quot;</span> <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">name</span><span class="p">,</span>
                <span class="s2">&quot;type&quot;</span> <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">type</span><span class="p">,</span>
                <span class="s2">&quot;year&quot;</span> <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">year</span><span class="p">,</span>
            <span class="p">]</span>
        <span class="p">);</span>

        <span class="c1">// Create a response</span>
        <span class="nv">$response</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Response</span><span class="p">();</span>

        <span class="c1">// Check if the insertion was successful</span>
        <span class="k">if</span> <span class="p">(</span><span class="nv">$status</span><span class="o">-&gt;</span><span class="na">success</span><span class="p">()</span> <span class="o">==</span> <span class="k">true</span><span class="p">)</span> <span class="p">{</span>
            <span class="c1">// Change the HTTP status</span>
            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setStatusCode</span><span class="p">(</span><span class="mi">201</span><span class="p">,</span> <span class="s2">&quot;Created&quot;</span><span class="p">);</span>

            <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">id</span> <span class="o">=</span> <span class="nv">$status</span><span class="o">-&gt;</span><span class="na">getModel</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">id</span><span class="p">;</span>

            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setJsonContent</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;status&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;OK&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;data&quot;</span>   <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="p">,</span>
                <span class="p">]</span>
            <span class="p">);</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
            <span class="c1">// Change the HTTP status</span>
            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setStatusCode</span><span class="p">(</span><span class="mi">409</span><span class="p">,</span> <span class="s2">&quot;Conflict&quot;</span><span class="p">);</span>

            <span class="c1">// Send errors to the client</span>
            <span class="nv">$errors</span> <span class="o">=</span> <span class="p">[];</span>

            <span class="k">foreach</span> <span class="p">(</span><span class="nv">$status</span><span class="o">-&gt;</span><span class="na">getMessages</span><span class="p">()</span> <span class="k">as</span> <span class="nv">$message</span><span class="p">)</span> <span class="p">{</span>
                <span class="nv">$errors</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$message</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">();</span>
            <span class="p">}</span>

            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setJsonContent</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;status&quot;</span>   <span class="o">=&gt;</span> <span class="s2">&quot;ERROR&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;messages&quot;</span> <span class="o">=&gt;</span> <span class="nv">$errors</span><span class="p">,</span>
                <span class="p">]</span>
            <span class="p">);</span>
        <span class="p">}</span>

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="updating-data">
<h2>更新数据（Updating Data）<a class="headerlink" href="#updating-data" title="永久链接至标题">¶</a></h2>
<p>The data update is similar to insertion. The &#8220;id&#8221; passed as parameter indicates what robot must be updated:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Http\Response</span><span class="p">;</span>

<span class="c1">// Updates robots based on primary key</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">put</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots/{id:[0-9]+}&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">(</span><span class="nv">$id</span><span class="p">)</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$app</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$robot</span> <span class="o">=</span> <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">request</span><span class="o">-&gt;</span><span class="na">getJsonRawBody</span><span class="p">();</span>

        <span class="nv">$phql</span> <span class="o">=</span> <span class="s2">&quot;UPDATE Robots SET name = :name:, type = :type:, year = :year: WHERE id = :id:&quot;</span><span class="p">;</span>

        <span class="nv">$status</span> <span class="o">=</span> <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">modelsManager</span><span class="o">-&gt;</span><span class="na">executeQuery</span><span class="p">(</span>
            <span class="nv">$phql</span><span class="p">,</span>
            <span class="p">[</span>
                <span class="s2">&quot;id&quot;</span>   <span class="o">=&gt;</span> <span class="nv">$id</span><span class="p">,</span>
                <span class="s2">&quot;name&quot;</span> <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">name</span><span class="p">,</span>
                <span class="s2">&quot;type&quot;</span> <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">type</span><span class="p">,</span>
                <span class="s2">&quot;year&quot;</span> <span class="o">=&gt;</span> <span class="nv">$robot</span><span class="o">-&gt;</span><span class="na">year</span><span class="p">,</span>
            <span class="p">]</span>
        <span class="p">);</span>

        <span class="c1">// Create a response</span>
        <span class="nv">$response</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Response</span><span class="p">();</span>

        <span class="c1">// Check if the insertion was successful</span>
        <span class="k">if</span> <span class="p">(</span><span class="nv">$status</span><span class="o">-&gt;</span><span class="na">success</span><span class="p">()</span> <span class="o">==</span> <span class="k">true</span><span class="p">)</span> <span class="p">{</span>
            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setJsonContent</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;status&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;OK&quot;</span>
                <span class="p">]</span>
            <span class="p">);</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
            <span class="c1">// Change the HTTP status</span>
            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setStatusCode</span><span class="p">(</span><span class="mi">409</span><span class="p">,</span> <span class="s2">&quot;Conflict&quot;</span><span class="p">);</span>

            <span class="nv">$errors</span> <span class="o">=</span> <span class="p">[];</span>

            <span class="k">foreach</span> <span class="p">(</span><span class="nv">$status</span><span class="o">-&gt;</span><span class="na">getMessages</span><span class="p">()</span> <span class="k">as</span> <span class="nv">$message</span><span class="p">)</span> <span class="p">{</span>
                <span class="nv">$errors</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$message</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">();</span>
            <span class="p">}</span>

            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setJsonContent</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;status&quot;</span>   <span class="o">=&gt;</span> <span class="s2">&quot;ERROR&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;messages&quot;</span> <span class="o">=&gt;</span> <span class="nv">$errors</span><span class="p">,</span>
                <span class="p">]</span>
            <span class="p">);</span>
        <span class="p">}</span>

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="deleting-data">
<h2>删除数据（Deleting Data）<a class="headerlink" href="#deleting-data" title="永久链接至标题">¶</a></h2>
<p>The data delete is similar to update. The &#8220;id&#8221; passed as parameter indicates what robot must be deleted:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Http\Response</span><span class="p">;</span>

<span class="c1">// Deletes robots based on primary key</span>
<span class="nv">$app</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span>
    <span class="s2">&quot;/api/robots/{id:[0-9]+}&quot;</span><span class="p">,</span>
    <span class="k">function</span> <span class="p">(</span><span class="nv">$id</span><span class="p">)</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$app</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$phql</span> <span class="o">=</span> <span class="s2">&quot;DELETE FROM Robots WHERE id = :id:&quot;</span><span class="p">;</span>

        <span class="nv">$status</span> <span class="o">=</span> <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">modelsManager</span><span class="o">-&gt;</span><span class="na">executeQuery</span><span class="p">(</span>
            <span class="nv">$phql</span><span class="p">,</span>
            <span class="p">[</span>
                <span class="s2">&quot;id&quot;</span> <span class="o">=&gt;</span> <span class="nv">$id</span><span class="p">,</span>
            <span class="p">]</span>
        <span class="p">);</span>

        <span class="c1">// Create a response</span>
        <span class="nv">$response</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Response</span><span class="p">();</span>

        <span class="k">if</span> <span class="p">(</span><span class="nv">$status</span><span class="o">-&gt;</span><span class="na">success</span><span class="p">()</span> <span class="o">==</span> <span class="k">true</span><span class="p">)</span> <span class="p">{</span>
            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setJsonContent</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;status&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;OK&quot;</span>
                <span class="p">]</span>
            <span class="p">);</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
            <span class="c1">// Change the HTTP status</span>
            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setStatusCode</span><span class="p">(</span><span class="mi">409</span><span class="p">,</span> <span class="s2">&quot;Conflict&quot;</span><span class="p">);</span>

            <span class="nv">$errors</span> <span class="o">=</span> <span class="p">[];</span>

            <span class="k">foreach</span> <span class="p">(</span><span class="nv">$status</span><span class="o">-&gt;</span><span class="na">getMessages</span><span class="p">()</span> <span class="k">as</span> <span class="nv">$message</span><span class="p">)</span> <span class="p">{</span>
                <span class="nv">$errors</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$message</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">();</span>
            <span class="p">}</span>

            <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setJsonContent</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;status&quot;</span>   <span class="o">=&gt;</span> <span class="s2">&quot;ERROR&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;messages&quot;</span> <span class="o">=&gt;</span> <span class="nv">$errors</span><span class="p">,</span>
                <span class="p">]</span>
            <span class="p">);</span>
        <span class="p">}</span>

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="testing-our-application">
<h2>测试应用（Testing our Application）<a class="headerlink" href="#testing-our-application" title="永久链接至标题">¶</a></h2>
<p>Using <a class="reference external" href="http://zh.wikipedia.org/wiki/CURL">curl</a> we&#8217;ll test every route in our application verifying its proper operation.</p>
<p>Obtain all the robots:</p>
<div class="highlight-bash"><div class="highlight"><pre>curl -i -X GET http://localhost/my-rest-api/api/robots

HTTP/1.1 <span class="m">200</span> OK
Date: Tue, <span class="m">21</span> Jul <span class="m">2015</span> 07:05:13 GMT
Server: Apache/2.2.22 <span class="o">(</span>Unix<span class="o">)</span> DAV/2
Content-Length: 117
Content-Type: text/html<span class="p">;</span> <span class="nv">charset</span><span class="o">=</span>UTF-8

<span class="o">[{</span><span class="s2">&quot;id&quot;</span>:<span class="s2">&quot;1&quot;</span>,<span class="s2">&quot;name&quot;</span>:<span class="s2">&quot;Robotina&quot;</span><span class="o">}</span>,<span class="o">{</span><span class="s2">&quot;id&quot;</span>:<span class="s2">&quot;2&quot;</span>,<span class="s2">&quot;name&quot;</span>:<span class="s2">&quot;Astro Boy&quot;</span><span class="o">}</span>,<span class="o">{</span><span class="s2">&quot;id&quot;</span>:<span class="s2">&quot;3&quot;</span>,<span class="s2">&quot;name&quot;</span>:<span class="s2">&quot;Terminator&quot;</span><span class="o">}]</span>
</pre></div>
</div>
<p>Search a robot by its name:</p>
<div class="highlight-bash"><div class="highlight"><pre>curl -i -X GET http://localhost/my-rest-api/api/robots/search/Astro

HTTP/1.1 <span class="m">200</span> OK
Date: Tue, <span class="m">21</span> Jul <span class="m">2015</span> 07:09:23 GMT
Server: Apache/2.2.22 <span class="o">(</span>Unix<span class="o">)</span> DAV/2
Content-Length: 31
Content-Type: text/html<span class="p">;</span> <span class="nv">charset</span><span class="o">=</span>UTF-8

<span class="o">[{</span><span class="s2">&quot;id&quot;</span>:<span class="s2">&quot;2&quot;</span>,<span class="s2">&quot;name&quot;</span>:<span class="s2">&quot;Astro Boy&quot;</span><span class="o">}]</span>
</pre></div>
</div>
<p>Obtain a robot by its id:</p>
<div class="highlight-bash"><div class="highlight"><pre>curl -i -X GET http://localhost/my-rest-api/api/robots/3

HTTP/1.1 <span class="m">200</span> OK
Date: Tue, <span class="m">21</span> Jul <span class="m">2015</span> 07:12:18 GMT
Server: Apache/2.2.22 <span class="o">(</span>Unix<span class="o">)</span> DAV/2
Content-Length: 56
Content-Type: text/html<span class="p">;</span> <span class="nv">charset</span><span class="o">=</span>UTF-8

<span class="o">{</span><span class="s2">&quot;status&quot;</span>:<span class="s2">&quot;FOUND&quot;</span>,<span class="s2">&quot;data&quot;</span>:<span class="o">{</span><span class="s2">&quot;id&quot;</span>:<span class="s2">&quot;3&quot;</span>,<span class="s2">&quot;name&quot;</span>:<span class="s2">&quot;Terminator&quot;</span><span class="o">}}</span>
</pre></div>
</div>
<p>Insert a new robot:</p>
<div class="highlight-bash"><div class="highlight"><pre>curl -i -X POST -d <span class="s1">&#39;{&quot;name&quot;:&quot;C-3PO&quot;,&quot;type&quot;:&quot;droid&quot;,&quot;year&quot;:1977}&#39;</span>
    http://localhost/my-rest-api/api/robots

HTTP/1.1 <span class="m">201</span> Created
Date: Tue, <span class="m">21</span> Jul <span class="m">2015</span> 07:15:09 GMT
Server: Apache/2.2.22 <span class="o">(</span>Unix<span class="o">)</span> DAV/2
Content-Length: 75
Content-Type: text/html<span class="p">;</span> <span class="nv">charset</span><span class="o">=</span>UTF-8

<span class="o">{</span><span class="s2">&quot;status&quot;</span>:<span class="s2">&quot;OK&quot;</span>,<span class="s2">&quot;data&quot;</span>:<span class="o">{</span><span class="s2">&quot;name&quot;</span>:<span class="s2">&quot;C-3PO&quot;</span>,<span class="s2">&quot;type&quot;</span>:<span class="s2">&quot;droid&quot;</span>,<span class="s2">&quot;year&quot;</span>:1977,<span class="s2">&quot;id&quot;</span>:<span class="s2">&quot;4&quot;</span><span class="o">}}</span>
</pre></div>
</div>
<p>Try to insert a new robot with the name of an existing robot:</p>
<div class="highlight-bash"><div class="highlight"><pre>curl -i -X POST -d <span class="s1">&#39;{&quot;name&quot;:&quot;C-3PO&quot;,&quot;type&quot;:&quot;droid&quot;,&quot;year&quot;:1977}&#39;</span>
    http://localhost/my-rest-api/api/robots

HTTP/1.1 <span class="m">409</span> Conflict
Date: Tue, <span class="m">21</span> Jul <span class="m">2015</span> 07:18:28 GMT
Server: Apache/2.2.22 <span class="o">(</span>Unix<span class="o">)</span> DAV/2
Content-Length: 63
Content-Type: text/html<span class="p">;</span> <span class="nv">charset</span><span class="o">=</span>UTF-8

<span class="o">{</span><span class="s2">&quot;status&quot;</span>:<span class="s2">&quot;ERROR&quot;</span>,<span class="s2">&quot;messages&quot;</span>:<span class="o">[</span><span class="s2">&quot;The robot name must be unique&quot;</span><span class="o">]}</span>
</pre></div>
</div>
<p>Or update a robot with an unknown type:</p>
<div class="highlight-bash"><div class="highlight"><pre>curl -i -X PUT -d <span class="s1">&#39;{&quot;name&quot;:&quot;ASIMO&quot;,&quot;type&quot;:&quot;humanoid&quot;,&quot;year&quot;:2000}&#39;</span>
    http://localhost/my-rest-api/api/robots/4

HTTP/1.1 <span class="m">409</span> Conflict
Date: Tue, <span class="m">21</span> Jul <span class="m">2015</span> 08:48:01 GMT
Server: Apache/2.2.22 <span class="o">(</span>Unix<span class="o">)</span> DAV/2
Content-Length: 104
Content-Type: text/html<span class="p">;</span> <span class="nv">charset</span><span class="o">=</span>UTF-8

<span class="o">{</span><span class="s2">&quot;status&quot;</span>:<span class="s2">&quot;ERROR&quot;</span>,<span class="s2">&quot;messages&quot;</span>:<span class="o">[</span><span class="s2">&quot;Value of field &#39;type&#39; must be part of</span>
<span class="s2">    list: droid, mechanical, virtual&quot;</span><span class="o">]}</span>
</pre></div>
</div>
<p>Finally, delete a robot:</p>
<div class="highlight-bash"><div class="highlight"><pre>curl -i -X DELETE http://localhost/my-rest-api/api/robots/4

HTTP/1.1 <span class="m">200</span> OK
Date: Tue, <span class="m">21</span> Jul <span class="m">2015</span> 08:49:29 GMT
Server: Apache/2.2.22 <span class="o">(</span>Unix<span class="o">)</span> DAV/2
Content-Length: 15
Content-Type: text/html<span class="p">;</span> <span class="nv">charset</span><span class="o">=</span>UTF-8

<span class="o">{</span><span class="s2">&quot;status&quot;</span>:<span class="s2">&quot;OK&quot;</span><span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="conclusion">
<h2>结束语（Conclusion）<a class="headerlink" href="#conclusion" title="永久链接至标题">¶</a></h2>
<p>As we have seen, develop a RESTful API with Phalcon is easy. Later in the documentation we&#8217;ll explain in detail how to
use micro applications and the <a class="reference internal" href="phql.html"><em>PHQL</em></a> language.</p>
</div>
</div>


                    </div>
                  </div>
                </div>
            </div>
          </td>
        </tr>
      </table>
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="samples.html" title="示例列表（List of examples）"
             >下一页</a> |</li>
        <li class="right" >
          <a href="tutorial-vokuro.html" title="Tutorial 6: Vökuró"
             >上一页</a> |</li> 
      </ul>
    </div>

      <div class="prefooter">
  <div class="container">
      <div class="row">
          <div class="col-sm-3 text-right">
              <span>Follow along:</span>
          </div>
          <div class="col-sm-6 text-center">
              <a href="https://twitter.com/phalconphp" alt="Twitter" class="btn-social btn-social-twitter"><i class="icon-twitter"></i></a>
              <a href="https://www.facebook.com/pages/Phalcon-Framework/134230726685897" alt="Facebook" class="btn-social btn-social-facebook"><i class="icon-facebook"></i></a>
              <a href="https://plus.google.com/102376109340560896457" alt="Google+" class="btn-social btn-social-googleplus"><i class="icon-googleplus"></i></a>
              <a href="https://github.com/phalcon/cphalcon" alt="Github" class="btn-social btn-social-github"><i class="icon-github"></i></a>
          </div>
          <div class="col-sm-3">
          </div>
      </div>
  </div>

</div>
<footer class="footer">
    <div class="container">
        <div class="row">
            <div class="col-xs-4 col-sm-3">
                <h4>Download</h4>
                <ul>
                    <li><a href="/download">Installing Phalcon PHP</a></li>
                    <li><a href="http://docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                    <li><a href="http://api.phalconphp.com">API</a></li>
                    <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html">Tutorial</a></li>
                    <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html#sample-applications">Sample Applications</a></li>
                </ul>
            </div>
            <div class="col-xs-4 col-sm-3">
                <h4>Community</h4>
                <ul>
                    <li><a href="http://forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                    <li><a href="https://github.com/phalcon/cphalcon">GitHub</a></li>
                    <li><a href="https://github.com/phalcon/cphalcon/issues">Issue Tracker</a></li>
                    <li><a href="http://stackoverflow.com/questions/tagged/phalcon">Stack Overflow</a></li>
                    <li><a href="/en/testimonials">Testimonials</a></li>
                    <li><a href="http://builtwith.phalconphp.com/">Built with Phalcon</a></li>
                    <li><a href="http://store.phalconphp.com/">Store</a></li>
                </ul>
            </div>
            <div class="col-xs-4 col-sm-2">
                <h4>About</h4>
                <ul>
                    <li><a class="link-black" href="http://blog.phalconphp.com/">Blog</a></li>
                    <li><a href="/en/about">About</a></li>
                    <li><a href="/en/team">Team</a></li>
                    <li><a href="/en/roadmap">Roadmap</a></li>
                    <li><a href="/en/donate">Donate</a></li>
                    <li><a href="/en/consulting">Consulting</a></li>
                    <li><a href="/en/hosting">Hosting</a></li>
                </ul>
            </div>
            <div id="license-spaccer" class="visible-xs"></div>
            <div id="license-wrapper" class="col-xs-12 col-sm-4">
                <p class="license">
                    Phalcon is an open-source PHP framework <br>built as a C-extension. It is available under the <br>
                    <a href="http://opensource.org/licenses/BSD-3-Clause" target="_blank">new BSD License</a>.
                    <br>
                    <br>
                    Except where otherwise noted, content on this site is licensed under the
                    <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 License.</a>
                </p>
            </div>
        </div>
    </div>
</footer>

    </div>
    <script type="text/javascript">
    $(window).on("load", function(){
      var cx = '009733439235723428699:lh9ltjgvdz8';
      var gcse = document.createElement('script');
      gcse.type = 'text/javascript';
      gcse.async = true;
      gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(gcse, s);
    });
    </script>

  </body>
</html>